<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Home\CategoryService;
use App\Services\Home\ClientTaskService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class HomeController extends Controller
{
    protected $request;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 获取所有分类
     * @param CategoryService $categoryService
     * @return \Illuminate\Http\JsonResponse
     */
    public function categories(CategoryService $categoryService){
        $result = $categoryService->getCategories($this->request->input());
        return $result;
    }

    /**
     * 获取分类下产品
     * @param CategoryService $categoryService
     * @return \Illuminate\Http\JsonResponse
     */
    public function products(CategoryService $categoryService){
        $result = $categoryService->getProducts($this->request->input());
        return $result;
    }

    public function product_info(CategoryService $categoryService){
        $result = $categoryService->getProductInfo($this->request->input());
        return $result;
    }

    public function task_product(ClientTaskService $taskService){
        $result = $taskService->getTaskProduct($this->request->input());
        return $result;
    }

   public function upload(Request $request){
       if ($request->hasFile('file')) {
           $file = $request->file('file');
           $file_name = md5(env('app_key').time()).'.'.$file->getClientOriginalExtension();
           $store_path = 'images/'.date('Y-m');

           // 自动生成一个唯一文件名
           $path = Storage::putFile($store_path, $file);
           if (!file_exists($store_path)){
               mkdir($store_path,0777,true);
           }
           if ($file->move($store_path,$file_name)){
               return uploadSuccess('上传成功！',Storage::url($path));
           }else{
               return Error('上传失败！');
           }
       }else{
           return uploadSuccess('请选择文件！',$request->all());
       }

   }
}
